﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonTools;
using shop.Model;

namespace shop.Bll
{
    /// <summary> 正在编辑 </summary>
    public class EditingBll
    {

        /// <summary>
        /// 检查单据是否有人正在编辑, 排除当前用户
        /// </summary>
        /// <param name="docFlag">单据类型, 就是DocFlagEnum中的一项</param>
        /// <param name="id">单据id</param>
        /// <param name="user">当前用户</param>
        /// <returns>如果没人编辑,返回null</returns>
        public static Editings CheckDoc(string docFlag, int id, IUser user)
        {
            return DB.FSql.Select<Editings>().FirstOrDefault(a => a.DocName == docFlag && a.DocId == id);
        }

        /// <summary>
        /// 添加一项记录
        /// </summary>
        /// <param name="docFlag">单据类型, 就是DocFlagEnum中的一项</param>
        /// <param name="id">单据id</param>
        /// <param name="user">当前用户</param>
        /// <param name="editing"></param>
        /// <returns>如果成功添加返回true</returns>
        public static bool TryAdd(string docFlag, int id, IUser user, out Editings editing)
        {
            if (id <= 0)
            {
                editing = null;
                return true;
            }


            editing = DB.FSql.Select<Editings>().FirstOrDefault(a => a.DocName == docFlag && a.DocId == id);
            if (editing != null)
            {
                if (editing.UserName == user.Name && editing.UserComputer == user.ComputerInfo)
                {
                    //编辑人是自己, 可以继续编辑 (检查用户和计算机信息, 有时一个账户多人登陆)
                }
                else
                {
                    return false;  //已经有人在编辑了, 不能添加
                }
            }

            var time = DB.FSql.GetTime();
            DB.FSql.Insert(new Editings()
            {
                DocId = id,
                DocName = docFlag,
                UserName = user.Name,
                EditTime = time,
                UserComputer = user.ComputerInfo,
            }).ExecuteAffrows();


            return true;

        }

        /// <summary> 添加一项记录 </summary>
        public static bool TryAdd(IDoc doc, IUser user, out Editings editing)
        {
            return TryAdd(doc.DocFlag , doc.DocId, user, out editing);  //调用重载的方法
        }


        /// <summary>
        /// 清空一个单据的正在编辑记录
        /// </summary>
        /// <param name="docFlag"></param>
        /// <param name="id"></param>
        public static void Clear(string docFlag, int id)
        {
            if (string.IsNullOrEmpty(docFlag) || id ==0 )
                return;

            DB.FSql.Delete<Editings>()
               .Where(a => a.DocId == id && a.DocName == docFlag)
               .ExecuteAffrows();

        }

        /// <summary> 清空一个单据的正在编辑记录 </summary>
        public static void Clear(IDoc doc, string docFlag)
        {
            if (doc == null)
                return;

            if (string.IsNullOrWhiteSpace(docFlag))
            {
                Clear(doc.DocFlag, doc.DocId);  //调用重载
            }
            else
            {
                Clear(docFlag, doc.DocId);
            }
        }



        /// <summary> 清空一个单据的正在编辑记录 </summary>
        public static void Clear(IDoc doc)
        {
            if (doc == null)
                return;
 
            Clear(doc.DocFlag, doc.DocId);  //调用重载
        }




        /// <summary>  </summary>
        public IEnumerable< dynamic> Query()
        {
            var list = DB.FSql.Select<Editings>()
                .ToList()
                .Select(a => new
                {
                    单据ID = a.DocId,
                    单据 = CommonTools.ToolConfig.GetDocumentName(a.DocName),
                    编辑时间 = a.EditTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                    编辑用户 = a.UserName,
                    编辑电脑 = a.UserComputer,
                })
                .ToList();
            
            return list;
        }


        /// <summary> 清空 </summary>
        public void ClearAll()
        {
            DB.FSql.Delete<Editings>()
                .Where(a => 1 == 1)
                .ExecuteAffrows();
        }




    }

}
